package com.tyfspring.beans.aware;

import com.tyfspring.beans.config.BeanPostProcessor;
import com.tyfspring.beans.exception.BeansException;
import com.tyfspring.context.ApplicationContext;

/**
 * @Description 包装处理器
 * @Author shallow
 * @Date 2023/4/11 20:53
 */

public class ApplicationContextAwareProcessor implements BeanPostProcessor {

    private final ApplicationContext applicationContext;

    public ApplicationContextAwareProcessor(ApplicationContext applicationContext) {
        this.applicationContext = applicationContext;
    }

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        if (bean instanceof ApplicationContextAware){
            ((ApplicationContextAware) bean).setApplicationContext(applicationContext);
        }
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        return bean;
    }
}
